package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.constant.OrderType;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.House;
import pers.cj.pdos.entity.OrderRule;
import pers.cj.pdos.entity.Store;
import pers.cj.pdos.service.IOrderRuleService;
import pers.cj.pdos.service.IStoreService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.StoreVo;
import pers.cj.pdos.vo.UserVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/organization")
public class StoreController extends BaseController {
    @Autowired
    private IStoreService storeService;

    @RequiresPermissions("store:query")
    @RequestMapping("/store")
    public String store(){
        return "org/store";
    }


    @RequiresPermissions("store:insert")
    @RequestMapping("/storeNew")
    public String storeNew(){
        return "org/storeNew";
    }


    @RequiresPermissions("store:insert")
    @RequestMapping("/storeEdit")
    public String storeEdit(Model model,String id){
        model.addAttribute("id",id);
        return "org/storeEdit";
    }

    @ResponseBody
    @RequiresPermissions("store:query")
    @RequestMapping("/store/getPageStores")
    public ResponseList getPageStores(int page, int limit, String searchContent) {
        Company company = getCompany();

        List<StoreVo> storeVoList = storeService.listVoPage(page, limit, company.getId(), searchContent);

        //返回数据
        return ResponseList.success(storeService.count(company.getId(), searchContent), storeVoList);
    }

    @ResponseBody
    @RequiresPermissions("store:insert")
    @RequestMapping("/store/add")
    public ResponseEntity add(@RequestBody Store store) {
        if (Utils.isEmpty(store.getNo())) {
            return ResponseEntity.fail("编号不能为空，请点击重新获取");
        }

        Company company = getCompany();
        if (storeService.checkName(store.getName(),company.getId())){
            return ResponseEntity.fail("门店名称已存在");
        }

        if (storeService.checkNo(store.getNo(),company.getId())){
            return ResponseEntity.fail("门店编号已被使用");
        }
        DataUtil.processEntity(company,store);
        storeService.insert(store);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("store:delete")
    @RequestMapping("/store/delete")
    public ResponseEntity delete(@RequestBody Store store) {
        try {
            Store old = storeService.getById(store.getId());
            if (old == null) {
                return ResponseEntity.fail("传入的id有误");
            }

            if (old.getSys()) {
                return ResponseEntity.fail("系统门店，无法删除");
            }

            DataUtil.processEntity(false, getCompany(), store);
            storeService.remove(store);
            return ResponseEntity.success();
        } catch (Exception exception) {
            return ResponseEntity.fail("已被使用，删除失败");
        }
    }

    @ResponseBody
    @RequiresPermissions("store:insert")
    @RequestMapping("/store/update")
    public ResponseData update(@RequestBody Store store) {
        Company company = getCompany();
        Store temp = storeService.getById(store.getId());
        if (temp==null){
            ResponseData.fail("id不正确");
        }

        if (!temp.getName().equals(store.getName()) && storeService.checkName(store.getName(),company.getId())){
            return ResponseData.fail("门店名称已存在");
        }

        if (!temp.getNo().equals(store.getNo()) && storeService.checkNo(store.getNo(),company.getId())){
            return ResponseData.fail("门店编号已存在");
        }

        DataUtil.processEntity(false,company, store);
        storeService.update(store);

        return ResponseData.success(storeService.getVoByIdAndHouseNormal(store.getId()));
    }

    @ResponseBody
    @RequestMapping("/store/getStores")
    public ResponseData getStores() {
        Company company = getCompany();
        UserVo userVo = getLoginUserVo();
        List<Store> storeList = new ArrayList<>();
        if (userVo.getStore().getSys()) {
            storeList = storeService.list(company.getId());
        } else {
            Store store = storeService.getById(userVo.getStoreId());
            storeList.add(store);
        }
        return ResponseData.success(storeList);
    }

    @ResponseBody
    @RequestMapping("/store/getVoById")
    public ResponseData getVoById(String id) {
        StoreVo storeVo = storeService.getVoById(id);
        if (storeVo==null){
            return ResponseData.fail("未获取到数据");
        }
        return ResponseData.success(storeVo);
    }
}
